package com.ica.android.ui.command;

import java.util.List;
import java.util.Vector;

import com.ica.R;
import com.ica.android.ui.command.CommandManagementActivity;
import com.ica.model.facade.CommandFacadeMockImpl;
import com.ica.model.command.AbstractCommand;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;

/**
 * @author     Hector
 * @uml.dependency   supplier="com.ica.android.ui.command.CommandActivity"
 */
public class CommandManagementActivity extends ListActivity {
	
	/**
	 * @uml.property  name="commandFacade"
	 * @uml.associationEnd  
	 */
	private CommandFacadeMockImpl commandFacade = null;
	/**
	 */
	private List<AbstractCommand> commands = null;
	/**
	 */
	private ArrayAdapter<AbstractCommand> arrayAdapter = null;
	static final int EDIT_COMMAND = 0;
	static final int NEW_COMMAND = 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Init AbstractCommand Facade
        commandFacade = CommandFacadeMockImpl.getInstance();

        // Set Content View based on AbstractCommand_tab layout
        setContentView(R.layout.command_tab);
        
        // Fill ListView with existing commands
        commands = new Vector<AbstractCommand>(commandFacade.getElements());
        arrayAdapter = new ArrayAdapter<AbstractCommand>(this, android.R.layout.simple_list_item_1, commands);
        setListAdapter(arrayAdapter); 
        
        // Register the ListView for Context Menu and for the OnItemClick event
        ListView lv = getListView();
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
              
        		Intent intent = new Intent(CommandManagementActivity.this, EditCommandActivity.class);
        		
        		AbstractCommand selectedAbstractCommand = (AbstractCommand) getListAdapter().getItem(position);
	        	
	        	// The class AbstractCommand and its attributes are both serializables
        		intent.putExtra("commandId", selectedAbstractCommand.getId());   	
	        	startActivityForResult(intent, EDIT_COMMAND);
            }
          });
    	  registerForContextMenu(lv);
    }
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if ((requestCode==EDIT_COMMAND) || requestCode==NEW_COMMAND) {
            if (resultCode == RESULT_OK) {

            	commands.clear();
            	commands.addAll(commandFacade.getElements());
            	arrayAdapter.notifyDataSetChanged();
	            Toast.makeText(this, "Command saved", Toast.LENGTH_SHORT).show();
            }
        }
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.command_menu, menu);
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	        case R.id.addCommand:
	        	Intent intent = new Intent(this, NewCommandActivity.class);
	        	startActivityForResult(intent, NEW_COMMAND);
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
	    super.onCreateContextMenu(menu, v, menuInfo);
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.command_context_menu, menu);
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
	    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
	    AbstractCommand selectedAbstractCommand = (AbstractCommand) getListAdapter().getItem(info.position);
	    switch (item.getItemId()) {
	        case R.id.deleteCommand:
	        	AlertDialog alert = createDeleteAbstractCommandDialog(selectedAbstractCommand);
	        	alert.show();
	            return true;
	        default:
	            return super.onContextItemSelected(item);
	    }
	}
	
	private AlertDialog createDeleteAbstractCommandDialog(final AbstractCommand AbstractCommand) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(R.string.delete_command_dialog_name)
		       .setCancelable(false)
		       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {		                
		        	   CommandManagementActivity.this.commandFacade.removeElement(AbstractCommand.getId());
		        	   CommandManagementActivity.this.commands.remove(AbstractCommand);
		        	   CommandManagementActivity.this.arrayAdapter.notifyDataSetChanged();
		           }
		       })
		       .setNegativeButton("No", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		                
		        	   dialog.cancel();
		           }
		       });
		return builder.create();
	}
}
